package com.woniu.new_orange.personnel_model_wu.controller;

import com.woniu.new_orange.entity.Refund;
import com.woniu.new_orange.personnel_model_wu.Dto.OrderDto;
import com.woniu.new_orange.personnel_model_wu.VO.OrderVo;
import com.woniu.new_orange.personnel_model_wu.service.RefundService;
import com.woniu.new_orange.util.ObjectExcelRead;
import com.woniu.new_orange.util.ObjectExcelView;
import com.woniu.new_orange.util.PageData;
import com.woniu.new_orange.util.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;


import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Program: new_orange->Refund
 * @Description:
 * @Author: 13488
 * @Date: 2023-02-07 10:07
 * @Version： 1.0
 **/
@RestController
@RequestMapping("refundControllers")
@Api(tags = "退款")
public class RefundController {
    @Resource
    private RefundService refundService;
    @GetMapping
public Result getAll(){
      List<Refund> refunds = refundService.selectAll();
      return  Result.ok().data("refundList",refunds);
    }
    @PostMapping("")
    public Result add1(@RequestBody Refund refund){
//        List<Refund> refunds = refundService.selectAll();
        int i = refundService.insertSelective1(refund);
        return  Result.ok();
    }

    @ApiOperation(value = "/导入退款表")
    @RequestMapping(value = "importExcel/{userIdS}")
    public Result importExcel(MultipartFile file, HttpServletRequest request) throws Exception{

        if (null != file && !file.isEmpty()) {
            //执行读EXCEL操作,读出的数据导入List 2:从第2行开始；0:从第A列开始；0:第0个sheet
            List<PageData> listPd = (List) ObjectExcelRead.readExcel(file.getInputStream(), 1, 0, 0);
            for (PageData pageData : listPd) {
               Refund refund=new Refund();
                String strId = pageData.getString("var0");
                int id=0;
                if(strId.indexOf(".")!=-1){
                    id = Integer.parseInt(strId.substring(0,strId.indexOf(".")));
                }else {
                    id = Integer.parseInt(strId);
                }
                refund.setRefundId(id);
                String  strMoney =pageData.getString("var1");
                BigDecimal money=new BigDecimal(strMoney);;
                refund.setRefundMoney(money);
                refund.setRefundState(pageData.getString("var2"));
                refund.setRefundCreateTime(new SimpleDateFormat("yyyy-MM-dd").parse(pageData.getString("var3")));
                refund.setRefundDesc(pageData.getString("var4"));
                String strUserId = pageData.getString("var5");
                int userId=0;
                if(strUserId.indexOf(".")!=-1){
                    userId = Integer.parseInt(strUserId.substring(0,strId.indexOf(".")));
                }else {
                    userId = Integer.parseInt(strId);
                }
                refund .setUserId(userId);

                refundService.insertSelective(refund);
            }

        }
        return Result.ok("导入成功");
    }
//    @ApiOperation(value = "导出模板",notes ="可以导出模板" )
//    @RequestMapping(value = "exportExcel")
//    public ModelAndView exportExcel(@RequestBody ) {
//        ModelAndView mv = new ModelAndView();
//        try {
//            Map<String, Object> dataMap = new HashMap<>();
//            //构造列名称
//            List<String> titles = new ArrayList<>();
//            titles.add("退款编号"); // 1
//            titles.add("退款金额"); // 2
//            titles.add("退款状态"); // 3
//            titles.add("退款申请时间（举例：2023-02-07 00:00:00）"); // 4
//            titles.add("退款备注");
//            dataMap.put("titles", titles);
//            //得到被导出的数据
//            List<OrderVo> orderDtoList = refundService.selectAll(order);
//            List<PageData> varList = new ArrayList<>();
//            for (OrderVo orderVo : orderDtoList) {
//                PageData vpd = new PageData();
//                vpd.put("var1",orderVo.getOrderFormNo());
//                vpd.put("var2",orderVo.getOrderFormOrigin());
//                vpd.put("var3",orderVo.getOrderFormDestination());
//                vpd.put("var4",orderVo.getOrderFormBudget());
//                vpd.put("var4",orderVo.getOrderFormDate());
//                vpd.put("var4",orderVo.getOrderFormDesc());
//                varList.add(vpd);
//            }
//            dataMap.put("varList", varList);
//            ObjectExcelView erv = new ObjectExcelView();
//            mv = new ModelAndView(erv, dataMap);
//
//        } catch (Exception e) {
//            e.printStackTrace();
//        }
//        return mv;
//    }


}
